package com.adjy.pathbuilders.commons;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.jbox2d.common.Vec2;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.adjy.pathbuilders.rendering.Point;
import com.facebook.widget.ProfilePictureView;
//import com.facebook.widget.ProfilePictureView;

public class Util {
	
	public static float getDistance(float x, float y, float x2, float y2) {
		return (float) Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
	}
	
	public static Vec2 normalize(Vec2 v) {
		
		Vec2 normalized = new Vec2();
		double length = Math.sqrt(v.x * v.x + v.y * v.y);
 		
		normalized.x = (float) (v.x / length);
		normalized.y = (float) (v.y / length);
		return normalized;
	}
	
	public static Point centerTextOnCanvas(Canvas canvas, Paint paint, float x, float y, float width, float height, String text) {
	    Rect bounds = new Rect();
	    paint.getTextBounds(text, 0, text.length(), bounds);

	    return new Point(
	    		x + width / 2 - paint.measureText(text) / 2,
	    		y + height / 2 + (bounds.bottom-bounds.top) / 2);
	}
	
	public static Bitmap getBitmapFromView(View view) {
	    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
	    Canvas canvas = new Canvas(returnedBitmap);
	    Drawable bgDrawable =view.getBackground();
	    if (bgDrawable!=null) 
	        bgDrawable.draw(canvas);
	    else 
	        canvas.drawColor(Color.WHITE);
	    view.draw(canvas);
	    return returnedBitmap;
	}
	
	public static Bitmap getBitmapFromView(ProfilePictureView profilePictureFB) {
		ImageView fbImage;
		Drawable dw;
		Bitmap b = null;
		for (int i = 0; i < profilePictureFB.getChildCount(); i++) {
			fbImage = ((ImageView)profilePictureFB.getChildAt(i));
			dw = fbImage.getDrawable();
			if(dw != null) {
				b = ((BitmapDrawable) dw).getBitmap();
			}
		}
		return b;
	}
	
	public static Bitmap getBitmapFromURL(String src) {
	    try {
	        URL url = new URL(src);
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoInput(true);
	        connection.connect();
	        InputStream input = connection.getInputStream();
	        return BitmapFactory.decodeStream(input);
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    }
	}
	
	public static Bitmap loadBitmap(String url) {
		try {
			InputStream is = (InputStream)new URL(url).getContent();
			return BitmapFactory.decodeStream(is);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

}
